/********************************************
* Copyrigt (C) rumi
* Module: bsp_gpio.c
* ESP32 GPIO
*********************************************/
#include "bsp_gpio.h"
// static const char *TAG = "bsp_gpio.c";

//*****************ISRISR***********************************/
// APP ISR Hanlder include
//*****************ISRISR  Function************************/
static void IRAM_ATTR default_isr_handler(void* arg) {
    printf("default interupt emit para:%s\n", (char*)arg);
}

// static void IRAM_ATTR custom_isr1(void* arg) {
//     printf("interupt1 triger para:%s\n", (char*)arg);
// }

/***********************INIT******************************/
void Bsp_Init_ALL_GPIO(void) {
    const gpio_init_config_t config_table[] = {
        // GPIO         MODE             PULLUP_EN              PULLDOWN_EN            INTR_TYPE
        //{ GPIO_NUM_5,  GPIO_MODE_INPUT,  GPIO_PULLUP_ENABLE,  GPIO_PULLDOWN_DISABLE, GPIO_INTR_DISABLE, NULL, NULL }, 
        //{ GPIO_NUM_6,  GPIO_MODE_INPUT,  GPIO_PULLUP_DISABLE, GPIO_PULLDOWN_ENABLE,  GPIO_INTR_ANYEDGE, default_isr_handler, (void*)GPIO_NUM_6 }, 
        // { GPIO_NUM_7,  GPIO_MODE_INPUT,  GPIO_PULLUP_ENABLE,  GPIO_PULLDOWN_DISABLE, GPIO_INTR_NEGEDGE, custom_isr1,        (void*)"BTN1" }, 
        { GPIO_NUM_2, GPIO_MODE_INPUT_OUTPUT_OD, GPIO_PULLUP_DISABLE, GPIO_PULLDOWN_DISABLE, GPIO_INTR_DISABLE, NULL, NULL }, 
        { GPIO_NUM_4, GPIO_MODE_INPUT_OUTPUT_OD, GPIO_PULLUP_DISABLE, GPIO_PULLDOWN_DISABLE, GPIO_INTR_DISABLE, NULL, NULL },
        //{ GPIO_NUM_21, GPIO_MODE_DEF_OUTPUT, GPIO_PULLUP_DISABLE, GPIO_PULLDOWN_DISABLE, GPIO_INTR_DISABLE, NULL, NULL },
    };

    gpio_install_isr_service(0);

    for (int i = 0; i < sizeof(config_table)/sizeof(config_table[0]); i++) {
        const gpio_init_config_t cfg = config_table[i];

        gpio_config_t io_conf = {
            .pin_bit_mask = (1ULL << cfg.num),
            .mode = cfg.mode,
            .pull_up_en = cfg.pull_up_en,
            .pull_down_en = cfg.pull_down_en,
            .intr_type = cfg.intr_type
        };
        gpio_config(&io_conf);

        if (cfg.intr_type != GPIO_INTR_DISABLE) {
            void* arg = cfg.isr_arg ? cfg.isr_arg : (void*)cfg.num;
            gpio_isr_handler_add(cfg.num, cfg.isr_handler ? cfg.isr_handler : default_isr_handler,arg);
        }
    }
}





/********************************************
 * End of file
*********************************************/